package com.rachel.liveplayer;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.rachel.liveplayer.callback.PermissionCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19.
 */

public abstract  class BaseActivity extends AppCompatActivity {
    private static PermissionCallback mCallback;
    private static final int  REQUESTCODE = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        initData();

        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    public abstract int getLayoutId();

    public abstract void initView();
    public abstract void initData();
    /**
     * 用于简化 findviewbyid，为什么不用那些注解框架？因为我有强迫症，看着不爽啊
     * @param resid
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int resid){
        View view = findViewById(resid);
        return (T) view;
    }



    //申请权限的方法
    public static void checkPermissions(String[]  permissions,PermissionCallback callback){
        mCallback = callback;
        Activity topActivity = ActivityCollector.getTopActivity();
        if (topActivity == null){
            return;
        }
        List<String> permissionList = new ArrayList<>();
        for (String permission : permissions){
            if (ContextCompat.checkSelfPermission(topActivity,permission) !=
                    PackageManager.PERMISSION_GRANTED){
                permissionList.add(permission);
            }
        }
        if (!permissionList.isEmpty()){
            ActivityCompat.requestPermissions((Activity) topActivity,
                    permissionList.toArray(new String[permissionList.size()]),REQUESTCODE);
        }else{
            mCallback.onGranted();
        }
    }
    //申请权限回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch(requestCode){
            case REQUESTCODE:
                if (grantResults.length > 0){
                    List<String> deniedPermissions = new ArrayList<>();
                    for (int i = 0; i < grantResults.length; i++) {
                        if (grantResults[i] != PackageManager.PERMISSION_GRANTED){
                            deniedPermissions.add(permissions[i]);
                            Toast.makeText(this, permissions[i]+"被拒绝了", Toast.LENGTH_SHORT).show();
                        }
                    }
                    if (!deniedPermissions.isEmpty()) {
                        mCallback.onDenied(deniedPermissions);
                    }else{
                        mCallback.onGranted();
                    }
                }
                break;
            default:
                break;
        }
    }
}
